package com.faithbj.printer;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.ServiceUI;
import javax.print.SimpleDoc;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;

import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

import com.faithbj.printer.service.RemoteInteractive;
import com.faithbj.printer.service.Impl.RemoteInteractiveImpl;

public class DocFlavorTest {
	
	public static void printTest() throws PrintException, IOException{
//		String url = "http://localhost/prpall/commonship/sff/UIPreConfirmQueryList.jsp?PoaCode=11111111111111111111111";
//		RemoteInteractive ri = new RemoteInteractiveImpl();
//		CloseableHttpClient  httpClient = HttpClients.createDefault();
//		String html = ri.getResource(url, httpClient);
//		httpClient.close();
		
		PrintService service = PrintServiceLookup.lookupDefaultPrintService();
		DocPrintJob job = service.createPrintJob();
		URL url = new URL( "http://localhost/prpall/commonship/sff/UIPreConfirmQueryList.jsp?PoaCode=11111111111111111111111");
		
		DocFlavor flavor = DocFlavor.URL.TEXT_HTML_HOST;
//		DocFlavor flavor = DocFlavor.STRING.TEXT_HTML;
//		DocFlavor flavor = DocFlavor.INPUT_STREAM.TEXT_HTML_UTF_8;
		Doc doc = new SimpleDoc(url.openStream(), flavor, null);
//		Doc doc = new SimpleDoc(html,flavor, null);
		PrintRequestAttributeSet attrs = new HashPrintRequestAttributeSet();
		attrs.add(new Copies(1));
//		PrintService selection = ServiceUI.printDialog( null, 100, 100, PrintServiceLookup.lookupPrintServices(null, null), service, null, attrs);
		job.print(doc, attrs);
	}
	
	public static void printImg() throws PrintException, IOException{
		PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
	      pras.add(new Copies(1));

//	      PrintService pss[] = PrintServiceLookup.lookupPrintServices(DocFlavor.INPUT_STREAM.GIF, pras);
//	      if (pss.length == 0)
//	        throw new RuntimeException("No printer services available.");
//	      PrintService ps = pss[0];
	      PrintService ps = PrintServiceLookup.lookupDefaultPrintService();
	      System.out.println("Printing to " + ps);

	      DocPrintJob job = ps.createPrintJob();
	      
	      FileInputStream fin = new FileInputStream("D:/screenshot.png");
	      Doc doc = new SimpleDoc(fin, DocFlavor.INPUT_STREAM.GIF, null);

	      job.print(doc, pras);

	      fin.close();
	}

	public static void main(String[] args) {
		try {
//			printTest();
			printImg();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (PrintException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
